package com.youyi.yyviewsdklibrary.View;

import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class MySpanText extends AppCompatTextView {
    private SpannableStringBuilder mStringBuilder = new SpannableStringBuilder();

    public interface OnSpanTextClickListener {
        void onClick(View view);
    }

    public MySpanText(Context context) {
        super(context);
    }

    public MySpanText(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public void init(String str, SpandTextBean... spandTextBeanArr) {
        boolean z;
        setText(str);
        this.mStringBuilder.append((CharSequence) str);
        int length = spandTextBeanArr.length;
        int i = 0;
        while (true) {
            if (i >= length) {
                z = true;
                break;
            }
            final SpandTextBean spandTextBean = spandTextBeanArr[i];
            String text = spandTextBean.getText();
            int indexOf = str.indexOf(text);
            int length2 = text.length() + indexOf;
            Log.d("MySpanText", "start00:" + indexOf);
            Log.d("MySpanText", "start00end:" + length2);
            if (indexOf == -1 || length2 == -1) {
                z = false;
            } else {
                this.mStringBuilder.setSpan(new ClickableSpan() {
                    /* class com.youyi.yyviewsdklibrary.View.MySpanText.AnonymousClass1 */

                    public void onClick(View view) {
                        if (spandTextBean.getOnSpanTextClickListener() != null) {
                            spandTextBean.getOnSpanTextClickListener().onClick(view);
                        }
                    }

                    public void updateDrawState(TextPaint textPaint) {
                        super.updateDrawState(textPaint);
                        textPaint.setColor(spandTextBean.getTextColor());
                        textPaint.setUnderlineText(false);
                    }
                }, indexOf, length2, 34);
                i++;
            }
        }
        if (z) {
            setHighlightColor(0);
            setText(this.mStringBuilder);
            setMovementMethod(LinkMovementMethod.getInstance());
        }
    }

    public static class SpandTextBean {
        private OnSpanTextClickListener mOnSpanTextClickListener;
        private String text;
        private int textColor;

        public SpandTextBean(String str, int i, OnSpanTextClickListener onSpanTextClickListener) {
            this.text = str;
            this.textColor = i;
            this.mOnSpanTextClickListener = onSpanTextClickListener;
        }

        public String getText() {
            return this.text;
        }

        public void setText(String str) {
            this.text = str;
        }

        public int getTextColor() {
            return this.textColor;
        }

        public void setTextColor(int i) {
            this.textColor = i;
        }

        public OnSpanTextClickListener getOnSpanTextClickListener() {
            return this.mOnSpanTextClickListener;
        }

        public void setOnSpanTextClickListener(OnSpanTextClickListener onSpanTextClickListener) {
            this.mOnSpanTextClickListener = onSpanTextClickListener;
        }
    }
}
